package com.hulytu.androidx.adapter.recyclerview

import com.hulytu.android.adapter.plugin.AdapterDataSetPlugin

/**
 *  kotlin 使用委托方式使用
 *
 * ```
 *
 * // 使用 typealias 进一步简化代码
 * typealias Adapter<DATA, VH> = BasicAdapterDelegate<DATA, VH>
 *
 * // 初始化
 * val adapter = object : Adapter<String, BasicViewHolder>(BasicDataSetAdapterPlugin()) {
 *      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BasicViewHolder {}
 *      override fun render(holder: BasicViewHolder, data: String, position: Int) {}
 * }
 *
 * ```
 *
 */

abstract class BasicAdapterDelegate<DATA, VH : BasicViewHolder>(
    private val plugin: AdapterDataSetPlugin<DATA>

) : BasicViewAdapter<VH>(), AdapterDataSetPlugin<DATA> by plugin {

    override fun getItemCount() = plugin.size()

    override fun render(holder: VH, position: Int) {
        render(holder, get(position), position)
    }

    abstract fun render(holder: VH, data: DATA, position: Int)

    override fun isEmpty() = plugin.isEmpty

    init {
        bindAdapter()
    }

    private fun bindAdapter() {
        adapter = this
    }
}